package com.ruoyi.mq.listener;


import com.rabbitmq.client.Channel;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

/**
 * @author jierui
 * @package com.ruoyi.mq.listener
 * @date 2021/7/2-13:40
 * @description:
 */
@Component
@Slf4j
public class DemoTwoReceiver {

    @RabbitListener(concurrency = "1-10",containerFactory = "simpleRabbitListenerContainerFactory",bindings = @QueueBinding(
            value = @Queue(value = "demo-two",
                    durable="true"),
            exchange = @Exchange(value = "demo-exchange",
                    durable="true",
                    type= "direct",
                    ignoreDeclarationExceptions = "true"),
            key = "demo_two"
    )
    )
    @RabbitHandler
    public void onMessage(Message message, Channel channel) throws Exception {

        log.info("消费端Payload: " + message.getPayload());
        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        //手工ACK,获取deliveryTag
        channel.basicAck(deliveryTag, false);
    }
}
